<?php

/**
 * This is the model class for table "task_analyst".
 *
 * The followings are the available columns in table 'task_analyst':
 * @property string $id_task_analyst
 * @property integer $id_task
 * @property string $step_analyst
 * @property integer $status
 * @property string $note
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class TaskAnalyst extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TaskAnalyst the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'task_analyst';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_task', 'required'),
			array('id_task, special_analyst, status, del_flg', 'numerical', 'integerOnly'=>true),
			array('step_analyst', 'length', 'max'=>255),
			array('note, date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_task_analyst, id_task, step_analyst, status, note, date_add, date_upd, del_flg', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'task' => array(self::BELONGS_TO, 'Task', 'id_task'),
            'analyst_special' => array(self::HAS_ONE, 'TaskAnalystSpecial', 'id_task_analyst'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_task_analyst' => '#',
			'id_task' => 'Id Task',
			'step_analyst' => Yii::t('app', 'Step Analyst'),
			'status' => Yii::t('app', 'Status'),
			'note' => Yii::t('app', 'Comment'),
			'date_add' => Yii::t('app', 'Date Add'),
			'date_upd' => Yii::t('app', 'Date Update'),
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_task_analyst',$this->id_task_analyst,true);
		$criteria->compare('id_task',$this->id_task);
		$criteria->compare('step_analyst',$this->step_analyst,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('note',$this->note,true);
		$criteria->compare('date_add',$this->date_add,true);
		$criteria->compare('date_upd',$this->date_upd,true);
		$criteria->compare('del_flg',$this->del_flg);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * @param $id_task
     * @$dataProvider check return dataProvider
     * @return CActiveDataProvider
     */
    public function search_task($id_task, $dataProvider = null)
    {
        $criteria=new CDbCriteria;
//        $criteria->with = array('analyst_special');
        $criteria->addCondition('del_flg = 0');
        $criteria->addCondition('id_task = :id_task');
        $criteria->params[':id_task'] = $id_task;

        if(!is_null($dataProvider)){
            return TaskAnalyst::model()->findAll($criteria);
        }

        return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
            'criteria'=>$criteria,
        ));
    }

    /**
     * update time to save record
     * @return bool
     */
    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }
}